﻿using CollegeRevApp.Data;
using CollegeRevApp.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CollegeRevApp.Web.DataAdapters
{
    /// <summary>
    /// Database adapter for College and Review data.
    /// </summary>
    public class CollegeAppAdapter : ICollegeAppAdapter
    {

        public College AddCollege(College college)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Add(college);
                db.SaveChanges();
            }
            return college;
        }

        public List<College> GetAllColleges()
        {
            List<College> colleges;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                colleges = db.Colleges.OrderBy(x => x.Name).ToList();
            }
            return colleges;
        }

        public College GetCollege(int id)
        {
            College college;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Find(id);
            }
            return college;
        }

        public College EditCollege(int id, College college)
        {
            College dbCollege;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbCollege = db.Colleges.Find(id);
                dbCollege.SetProperties(college);
                db.SaveChanges();
            }
            return dbCollege;
        }

        public College DeleteCollege(int id)
        {
            College college;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                college = db.Colleges.Find(id);
                db.Reviews.RemoveRange(db.Reviews.Where(x => x.CollegeId == college.Id).ToList());
                db.Colleges.Remove(college);
                db.SaveChanges();
            }
            return college;
        }

        public Review AddReview(Review review)
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Add(review);
                db.SaveChanges();
            }
            return review;
        }

        public List<Review> GetAllReviews()
        {
            List<Review> reviews;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                reviews = db.Reviews.OrderBy(x => x.Id).ToList();
            }
            return reviews;
        }

        public List<Review> GetReviewsForCollege(int id)
        {
            List<Review> reviews;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                reviews = db.Reviews.Where(x => x.CollegeId == id).OrderBy(x => x.Id).ToList();
            }
            return reviews;
        }

        public Review EditReview(int id, Review review)
        {
            Review dbReview;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                dbReview = db.Reviews.Find(id);
                dbReview.SetProperties(review);
                db.SaveChanges();
            }
            return dbReview;
        }

        public Review DeleteReview(int id)
        {
            Review review;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                review = db.Reviews.Remove(db.Reviews.Find(id));
                db.SaveChanges();
            }
            return review;
        }


        public float GetAverageRating(int id)
        {
            float average;
            using(ApplicationDbContext db = new ApplicationDbContext())
            {
                List<Review> reviews = db.Reviews.Where(x => x.CollegeId == id).ToList();
                int total = 0, count = 0;

                foreach (var review in reviews)
                {
                    total += (int)review.EducationQuality;
                    total += (int)review.CampusLife;
                    total += (int)review.SafetySecurity;
                    total += (int)review.JobPlacement;

                    count += (int)review.EducationQuality == 0 ? 0 : 1;
                    count += (int)review.CampusLife == 0 ? 0 : 1;
                    count += (int)review.SafetySecurity == 0 ? 0 : 1;
                    count += (int)review.JobPlacement == 0 ? 0 : 1;
                }

                average = (float)total / (float)count;

            }
            return average;
        }
    }
}